package com.shengde.jinwawaforleader.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;


/**
 * Created by Administrator on 2016/12/7 0007.
 */

public class UmengShareUtil {
	//context 上下文
	//title 分享的标题
	//url分享的 图片界面的url地址
	//content 分享的内容
	//targeturl 分享的链接 地址

	/**
	 * 分享图片
	 * @param context
	 * @param title
	 * @param content
	 * @param url
	 * @param targeturl
	 */
	public static void share(final Context context, String title, String content, String url, String targeturl) {
		new ShareAction((Activity) context).setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
				.withText(content + "——来自金娃娃分享面板")
				.withMedia(new UMImage(context, url))
				.setCallback(new UMShareListener() {
					@Override
					public void onStart(SHARE_MEDIA share_media) {

					}

					@Override
					public void onResult(SHARE_MEDIA platform) {
						com.umeng.socialize.utils.Log.d("plat", "platform" + platform);
						if (platform.name().equals("WEIXIN_FAVORITE")) {
							Toast.makeText(context, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(context, " 分享成功啦", Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onError(SHARE_MEDIA share_media, Throwable throwable) {
						Toast.makeText(context, " 分享失败啦", Toast.LENGTH_SHORT).show();
						if (throwable != null) {
							com.umeng.socialize.utils.Log.d("throw", "throw:" + throwable.getMessage());
						}
					}
					@Override
					public void onCancel(SHARE_MEDIA share_media) {
						Toast.makeText(context, " 分享取消了", Toast.LENGTH_SHORT).show();

					}
				})
				.open();

	}

	/**
	 * 分享url
	 * @param context
	 * @param title
	 * @param content
	 * @param url
	 * @param targeturl
	 */
	public static void shareUrl(final Context context, String title, String content, String url, String targeturl) {
		UMWeb web = new UMWeb(targeturl);
		web.setTitle(title);//标题
		web.setThumb(new UMImage(context, url));  //缩略图
		web.setDescription(content + "——来自金娃娃分享面板");//描述
		new ShareAction((Activity) context).setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
				.withMedia(web)
				.setCallback(new UMShareListener() {
					@Override
					public void onStart(SHARE_MEDIA share_media) {

					}

					@Override
					public void onResult(SHARE_MEDIA platform) {
						com.umeng.socialize.utils.Log.d("plat", "platform" + platform);
						if (platform.name().equals("WEIXIN_FAVORITE")) {
							Toast.makeText(context, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(context, " 分享成功啦", Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onError(SHARE_MEDIA share_media, Throwable throwable) {
						Toast.makeText(context, " 分享失败啦", Toast.LENGTH_SHORT).show();
						if (throwable != null) {
							com.umeng.socialize.utils.Log.d("throw", "throw:" + throwable.getMessage());
						}
					}

					@Override
					public void onCancel(SHARE_MEDIA share_media) {
						Toast.makeText(context, " 分享取消了", Toast.LENGTH_SHORT).show();

					}
				})
				.open();

	}

}
